package wangxiaotao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import wangxiaotao.bean.vo.RoomVO;
import wangxiaotao.converter.RoomConverter;
import wangxiaotao.entity.Room;
import wangxiaotao.entity.RoomType;
import wangxiaotao.enums.OrderStatue;
import wangxiaotao.enums.RoomStatue;
import wangxiaotao.mapper.AdminMapper;
import wangxiaotao.mapper.RoomMapper;
import wangxiaotao.mapper.RoomTypeMapper;
import wangxiaotao.service.RoomService;
import wangxiaotao.tools.MinioTool;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-03-22
 */
@Service
@RequiredArgsConstructor
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    private final AdminMapper adminMapper;
    private final RoomConverter roomConverter;
    private final MinioTool minioTool;
    private final RoomTypeMapper roomTypeMapper;
    private final RoomMapper roomMapper;


    /**
     * 订单状态变化 修改房间状态
     * @param orderStatue
     */
    @Override
    public void changeRoomStatueByOrderStatue(Integer orderId,OrderStatue orderStatue,String orderRoomNo) {
        //更新条件构造
        UpdateWrapper<Room> wrapper = new UpdateWrapper<>();
        //根据不同的订单状态，更新房间状态
        switch (orderStatue.getStatue()){
            case 0: case 1: case 2: case 3:
            case 10: case 8: case 9: case 7:
                wrapper.set("room_statue", RoomStatue.FREE).eq("room_no",orderRoomNo);break;
            case 4: wrapper.set("room_statue", RoomStatue.LOCK).eq("room_no",orderRoomNo);break;
            case 5: wrapper.set("room_statue", RoomStatue.ALREADYIN).eq("room_no",orderRoomNo);break;
            case 6: wrapper.set("room_statue", RoomStatue.WAITCLEAN).eq("room_no",orderRoomNo);break;
        }
        //执行更新
        this.update(wrapper);
    }

    /**
     * 添加房间数据
     * @param fileList
     * @param roomVO
     */
    @Override
    public void addRoom(MultipartFile[] fileList, RoomVO roomVO,String roomType) {
        //参数处理和封装
        String[] roomInfo = roomType.split(",");
        int floor = Integer.parseInt(roomInfo[2]);
        int roomTypeNo = Integer.parseInt(roomInfo[0]);
        String roomBed = roomInfo[1];
        //设置room_no xx+xxx 楼层+编号
        Integer roomNo = adminMapper.queryMaxRoomNo(floor);
        if (roomNo==null){//如果此楼层没有其他房间
            roomVO.setRoomNo(String.format("%02d",floor)+"001");
        }else {//根据房间编号最大来自动递增房间编号
            roomVO.setRoomNo(String.format("%02d",floor)+String.format("%03d", roomNo+1));
        }
        //保存房间图片到minio
        String roomImage = minioTool.saveBatchRoomImage(floor, roomVO.getRoomNo(), fileList);
        //对象类型转换
        Room room = roomConverter.toRoom(roomVO);
        room.setRoomTypeId(roomTypeNo);
        //根据类型和床型查找出房间类型的id
        QueryWrapper<RoomType> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","room_num","room_remain").eq("room_type_no",roomTypeNo).eq("room_bed",roomBed);
        RoomType realRoomType = roomTypeMapper.selectOne(queryWrapper);

        //更新房间数量 和剩余数量
        RoomType setRoomType = new RoomType();
        setRoomType.setRoomNum(realRoomType.getRoomNum()+1)
                .setRoomRemain(realRoomType.getRoomRemain()+1)
                .setId(realRoomType.getId());
        roomTypeMapper.updateById(setRoomType);
        //将房间和房间类型绑定
        room.setRoomTypeId(realRoomType.getId());
        //将回传的房间图片地址存储
        room.setRoomImages(roomImage);
        this.save(room);
    }

    @Override
    public List<Map<String, Object>> getRoomRemain() {
        return roomMapper.getRoomRemain();
    }

    @Override
    public List<Map<String, Object>> getRemainRoom() {
        return roomMapper.getRemainRoom();
    }

    /**
     * 判断房间状态能否被修改
     * @param roomVO
     */
    @Override
    public void judgeRoomStatue(RoomVO roomVO) {
        //查询被操作房间的状态
        Room realRoom = roomMapper.selectOne(new QueryWrapper<Room>().select("room_statue").eq("id", roomVO.getId()));
        //房间状态不能后退
        if (realRoom.getRoomStatue().getStatue()>roomVO.getRoomStatue()){
            throw new RuntimeException("房间状态不能后退！");
        }
    }
}
